# 数据库模块
from django.db import models
# 时间模块快
from django.utils import timezone
# 用户表
from django.contrib.auth.models import AbstractUser


# Create your models here.

# 基类表
class Base(models.Model):
    create_time = models.DateTimeField(default=timezone.now, null=True)  # 添加数据时，自动写入当前添加时间
    is_delete = models.IntegerField(default=0)  # 逻辑关联删除字段({0:未删除,1:已删除})

    class Meta:
        abstract = True
# 用户信息表
class User(Base):
    username = models.CharField(max_length=200, unique=True)  # 用户名
    password = models.CharField(max_length=200)  # 密码
    email = models.CharField(max_length=200, null=True)  # 邮箱
    phone = models.CharField(max_length=11, null=True)  # 手机号
    photo = models.CharField(max_length=200, default="1.jpg")  # 头像
    pid = models.IntegerField(default=1)  # 权限等级
    balance = models.IntegerField(default=0)  # 余额（单位：分）
    weibo = models.IntegerField(null=True)  # 微博ID
    dingding = models.IntegerField(null=True)  # 钉钉ID
    company_id = models.IntegerField(null=True)  # 所属公司
    class Meta:
        db_table = "user"
# 公司
class Companys(Base):
    # 公司名称
    name = models.CharField(max_length=500)
    # 所在城市
    address = models.CharField(max_length=500)
    # 公司logo
    logo = models.CharField(max_length=500,default='1.jpg')
    class Meta:
        db_table = 'companys'
# 权限表
class Power(Base):
    grade = models.CharField(max_length=20, null=True) 
    # 权限判定规则(1收货, 2发货, 3收货+发货, 4客服, 5管理员)
    power = models.IntegerField(null=True) # 
    class Meta:
        db_table = 'power'
# 用户收件地址
class UserAddress(Base):
    address = models.CharField(max_length=500)
    uid = models.IntegerField() # 关联用户,一个用户可以有多个地址
    address_dedfault = models.IntegerField(default=0)  # 是否为默认地址
    class Meta:
        db_table = 'user_address'
# 分类表
class Cates(Base):
    cate = models.CharField(max_length=200)
    class Meta:
        db_table = 'cates'
# 商品表
class Goods(Base):
    name = models.CharField(max_length=500) # 产品名称
    price = models.IntegerField(unique=True) # 产品价格
    cate = models.CharField(max_length=200) # json字符串存储cate_id
    uid = models.IntegerField(unique=True) # 商家id
    shipping_address = models.CharField(max_length=500) # 商品发货地址
    photo = models.CharField(max_length=200) # 商品图片, json字符串存储,取出时进行转换
    video = models.CharField(max_length=500) # 商品视频
    desc = models.TextField() # 商品简介
    
    class Meta:
        db_table = 'goods'
# 标签表
class Labels(Base):
    # 标签编号
    label_id = models.CharField(max_length=200,unique=True)
    # 货物类型
    goods_type = models.IntegerField()
    # 创建人
    create_user = models.IntegerField()
    # 使用者
    use_user = models.IntegerField()
    # 修改时间
    modify_time = models.DateTimeField(auto_now=True, null=True) 

    class Meta:
        db_table = 'labels'
# 货物类型表
class GoodsTypes(Base):
    # 货物类型名称
    name = models.CharField(max_length=200)
    # 延迟开始
    delayed = models.IntegerField(default=5)
    # 记录间隔
    interval = models.FloatField(default=0.5)
    # 最低温度
    min_temperature = models.IntegerField()
    # 最高温度
    max_temperature = models.IntegerField()
    # 报警事件
    alert = models.CharField(max_length=500)
    # 描述
    desc = models.CharField(max_length=500, null=True)
    # 设备类型
    equipment_id = models.IntegerField()
    class Meta:
        db_table = 'goodstypes'
# 设备类型表
class Equipments(Base):
    equipment_name = models.CharField(max_length=200)
    class Meta:
        db_table = 'equipment'
# 报警事件表
class AlertEvents(Base):
    # 报警事件类型,1 单温上限,2 单温下限, 3 单温累计事件
    alert_class = models.CharField(max_length=500)
    # 温度范围
    temperature = models.CharField(max_length=200)
    # 阈值, 数据库中以分钟为单位
    threshold = models.IntegerField(default=0)
    class Meta:
        db_table = 'alertevents'
# 订单表
class Orders(Base):
    # 构建发货单号, 构建一个随机且不重复的发货单号
    invoice = models.CharField(max_length=500)
    # 发货地址
    shipping_addresses = models.CharField(max_length=300, default="物资仓库")
    # 用户名
    username = models.CharField(max_length=200,default=None)
    # 联系人
    contacts = models.CharField(max_length=200,default=None)
    # 联系电话
    phone = models.IntegerField()
    # 公司地址
    address = models.CharField(max_length=500,default='China')
    # 货物类型
    goods_type_name = models.CharField(max_length=200,default='1')
    # 延迟开始
    delayed = models.IntegerField(default=5)
    # 记录间隔
    interval = models.FloatField(default=0.5)
    # 安全温度
    safe_temperature = models.CharField(max_length=200,default='0~0')
    # 最低温度
    # min_temperature = models.IntegerField()
    # # 最高温度
    # max_temperature = models.IntegerField()
    # 报警事件
    alert = models.CharField(max_length=500,default='[]')
    # 运单状态
    order_state = models.CharField(max_length=200,default='打包中')
    # 温度记录
    temperature_record = models.CharField(max_length=200,default="正常")
    # 结束时间
    end_time = models.DateField(auto_now=True)
    class Meta:
        db_table = 'orders'
# 温度记录表
class Temperatures(Base):
    # 运单号
    order_ID = models.CharField(max_length=200)
    # 当前温度
    temperature_new = models.CharField(max_length=200)
    class Meta:
        db_table = 'temperatures'
        unique_together = (
            ('order_ID','create_time'),
        )
# 物流经纬度表
class Locations(Base):
    # 运单发货单号
    Waybill_number = models.CharField(max_length=200)
    # 经度
    longitude = models.CharField(max_length=200)
    # 纬度
    latitude = models.CharField(max_length=200)
    class Meta:
        db_table = 'locations'
# 申请表
class ApplyInfo(Base):
    user = models.CharField(max_length=200)
    apply = models.IntegerField()
    # is_delete字段, 0 申请中, 1 通过, 2 失败
    class Meta:
        db_table = 'applys'

# 客户表
class Customers(Base):
    # 用户名
    username = models.CharField(max_length=200)
    # 联系人
    contacts = models.CharField(max_length=200)
    # 联系电话
    phone = models.IntegerField()
    # 公司地址
    address = models.CharField(max_length=500)

    class Meta:
        db_table = 'customers'


